package com.mud.mudcommune.view.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.RegisterEntry;
import com.mud.mudcommune.http.ApiService;
import com.mud.mudcommune.http.NetUtils;
import com.mud.mudcommune.view.fragment.MineFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SetupActivity extends AppCompatActivity implements View.OnClickListener, Callback<RegisterEntry> {
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.case_back)
    ImageView back;
    Unbinder unbinder;
    @BindView(R.id.exit_login)
    Button exit;
    private String username;
    @BindView(R.id.Exit_progressBar)
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mine_setup);
        unbinder = ButterKnife.bind(this);
        title.setText("设置");
    }

    @OnClick({R.id.case_back, R.id.exit_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.case_back:
                finish();
                break;
            case R.id.exit_login:
                //  progressBar.setVisibility(View.VISIBLE);
                username=MineFragment.getUserName();
                if("点击登录".equals(username)){
                    Toast.makeText(this,"当前用户未登录",Toast.LENGTH_SHORT).show();
                }else{
                    progressBar.setVisibility(View.VISIBLE);
                    NetUtils.createService(ApiService.class,ApiService.BASE_URL).getExit().enqueue(this);
                    break;
                }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public void onResponse(Call<RegisterEntry> call, Response<RegisterEntry> response) {
        RegisterEntry body = response.body();
        if (body.getStatus() == 200) {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(this, body.getMessage(), Toast.LENGTH_SHORT).show();//退出通知
            MineFragment.setUserName("点击登录");
            finish();
        }
    }

    @Override
    public void onFailure(Call<RegisterEntry> call, Throwable t) {
        t.printStackTrace();
    }
}
